import vue from 'vue'
import Vuex from 'vuex'
import router,{resetRouter} from "../router";
vue.use(Vuex)

function addNewRoute(menuList) {
    console.log(menuList)
    let routes=router.options.routes
    console.log(routes)
    routes.forEach(routerItem=>{
        if(routerItem.path=="/Index"){
            menuList.forEach(menu=>{
                let childRoute={
                    path:'/'+menu.menuClick,
                    name:menu.menuName,
                    meta:{
                        title:menu.menuName
                    },
                    component:()=>import('../components/'+menu.menuComponent)
                }
                routerItem.children.push(childRoute)
                }
            )
        }
    })
    resetRouter()
    router.addRoutes(routes)
}

export default new Vuex.Store({
    state:{
        menu:[]
    },
    mutations:{
        setMenu(sate,menuList){
            sate.menu=menuList
            
            addNewRoute(menuList)
        }
    },
    getters:{
        getMenu(state){
            return state.menu
        }
    }
})